package com.salton123.feature

import android.view.View
import com.kingja.loadsir.callback.Callback
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.salton123.arch.view.IMultiStatusView
import com.salton123.feature.multistatus.EmptyStatus
import com.salton123.feature.multistatus.ErrorStatus
import com.salton123.feature.multistatus.InitStatus
import com.salton123.feature.multistatus.LoadingStatus

/**
 * User: newSalton@outlook.com
 * Date: 2019/12/11 17:18
 * ModifyTime: 17:18
 * Description:
 */
class MultiStatusFeature : IFeature, IMultiStatusView {

    //状态页管理
    private var mBaseLoadService: LoadService<*>? = null
    lateinit var target: Any
    fun with(target: Any) {
        this.target = target
    }

    override fun onBind() {
        val builder = LoadSir.Builder()
            .addCallback(initStatus)
            .addCallback(emptyStatus)
            .addCallback(errorStatus)
            .addCallback(loadingStatus)
            .setDefaultCallback(initStatus.javaClass)
        mBaseLoadService = builder.build().register(target, target as Callback.OnReloadListener)
    }

    override fun onUnBind() {
        mBaseLoadService = null
    }

    override fun showInitLoadView() {
        if (mBaseLoadService != null) {
            mBaseLoadService!!.showCallback(initStatus.javaClass)
        }
    }

    override fun showNoDataView() {
        if (mBaseLoadService != null) {
            mBaseLoadService!!.showCallback(emptyStatus.javaClass)
        }
    }

    override fun showTransLoadingView() {
        if (mBaseLoadService != null) {
            mBaseLoadService!!.showCallback(loadingStatus.javaClass)
        }
    }

    override fun showNetWorkErrView() {
        if (mBaseLoadService != null) {
            mBaseLoadService!!.showCallback(errorStatus.javaClass)
        }
    }

    override fun showSuccessView() {
        if (mBaseLoadService != null) {
            mBaseLoadService!!.showSuccess()
        }
    }

    override fun onReload(v: View?) {
    }

    private val initStatus: Callback
        get() = InitStatus()

    private val loadingStatus: Callback
        get() = LoadingStatus()

    private val errorStatus: Callback
        get() = ErrorStatus()

    private val emptyStatus: Callback
        get() = EmptyStatus()
}